<?
//Globals

define('EVENTS_CAT_NAME', 'events');
define('BLOG_CAT_ID', 3);
define('FAQ_CAT_ID', 4);
define('TOP_TEXT_POST_ID', 85);
define('CONTACTUS_TEXT_POST_ID', 83); //Post ID for sidebar Contact us block in home page
define('WELCOME_TEXT_POST_ID', 89);
define('NEIGHBORHOOD_TEXT_POST_ID', 87);

//system

function check_blog_post($id)
{
     $str = "select * from `wp_term_relationships` where `object_id` = ".$id." and `term_taxonomy_id` = ".BLOG_CAT_ID;
     $sql = mysql_query($str);
     if(mysql_num_rows($sql) > 0)
     {
         $count = 0;

	     while($row = mysql_fetch_object($sql))
	     {
     	      if($id == $row->object_id)
     	      {     	      		$count++;
     	      }

	     }

     	 if($count > 0)
     	 {
     	 	return true;

     	 }
     	 else
     	 {     	 	return false;
     	 }
     }
     else
     {
     	return false;

     }
}

function prepare_q($s,$n)
{
	$s= strip_tags($s);
	$s=substr($s,0,$n);
	$l=strrpos($s,' ');
		if ($l!==false){
		   $s=substr($s,0,$l);
		}
	return $s.'&hellip;';
}

//blocks

function get_block_newsletter()
{?>

                                  <div id="sidebar-newsletter">
                                  		<div class="sidebar-block-bottom">
                                  			<div class="sidebar-block-cont">
                                  				<div class="sidebar-block-text">
                                  				   Praesent luctus, sesdfm a suscipit tempor, massa ligula accumf.
                                  				</div>
                                                <form method="POST" action="">
                                  				<div id="newsletter-input-bg">
                                  					<input name="text" type="text" id="news-inpt" value="Your e-mail" onclick="chstr_f();" onblur="chstr_b();">
                                  				</div>

                                  				<input type="submit" value="" id="newsletter-send" name="go">
                                                </form>
                                  			</div>
                                  		</div>
                                  </div>

<?
}

function get_block_calendar()
{?>
                                  <div id="sidebar-calendar">
                                  		<div class="sidebar-block-bottom">
                                  			<div class="sidebar-block-cont">
                                  				<div class="sidebar-block-text">
                                  				   Praesent luctus, sesdfm a suscipit tempor, massa ligula accumf.
                                  				</div>
                                               <!-- <div id="temp_calendar">

                                                </div>  -->
                                                <? ec3_get_calendar();?>

                                                <button id="calendar-learnmore"></button>

                                  			</div>
                                  		</div>
                                  </div>
<?
}

function get_block_blogcat()
{
?>
					<div id="sidebar-categories-top">
                    <div class="sidebar-block-bottom">
                    <div class="sidebar-block-cont">
                    <ul id="blog-categories">
<?
			$args = array(
			    'orderby'            => 'name',
			    'order'              => 'ASC',
			    'show_last_update'   => 1,
			    'style'              => 'list',
			    'show_count'         => 0,
			    'hide_empty'         => 0,
			    'use_desc_for_title' => 1,
			    'child_of'           => BLOG_CAT_ID,
			    'current_category'   => 4,
			    'hierarchical'       => true,
			    'title_li'           => __( '' ),
			    'number'             => NULL,
			    'echo'               => 1,
			    'depth'              => 1 );

           wp_list_categories($args);

?>
                                <!--

                                                 <ul id="blog-categories">
												    <li class="catitem"><a href="#">Praesent luctus, sesdfm a suspit</a></li>
												    <li class="catitem"><a href="#">Tempor, massa ligula accumf</a></li>
												    <li class="catitem"><a href="#">San erat, non venenatis ipsum</a></li>
												    <li class="catitem-last"><a href="#">Purus ac nulla. Maecenas dui</a></li>
												</ul> -->
					</ul>
                    </div>
                    </div>
                    </div>
<?
}

function get_block_bloglinks()
{

?>

				                 <div id="sidebar-links-top">
                                  	<div class="sidebar-block-bottom">
                                  		<div class="sidebar-block-cont">

                                                 <ul id="blog-links">
<?
						$args = array(
					    'orderby'          => 'name',
					    'order'            => 'ASC',
					    'limit'            => -1,
					    'hide_invisible'   => 1,
					    'show_updated'     => 0,
					    'echo'             => 1,
					    'categorize'       => 0,
					    'title_li'         => __(''),
					    'class'            => 'cat-item',
					    'before'		   => '<li class=cat-item>',
					    'after'		   => '</li>'
						 );

						wp_list_bookmarks( $args );

?>

											<!--	    <li class="linkitem"><a href="#">Praesent luctus, sesdfm suspit</a></li>
												    <li class="linkitem"><a href="#">Tempor, massa ligula accumf</a></li>
												    <li class="linkitem"><a href="#">San erat, non venenatis ipsum</a></li>
												    <li class="linkitem-last"><a href="#">Purus ac nulla. Maecenas dui</a></li>-->
												</ul>
                                  			</div>
                                  		</div>
                                  </div>
<?
}

function get_block_blogarchive()
{
?>

                                  <div id="sidebar-archives-top">
                                  		<div class="sidebar-block-bottom">
                                  			<div class="sidebar-block-cont">
                                                 <ul id="blog-arch">

<?
					$args = array(
				    'type'            => 'monthly',
				    'format'          => 'html',
				    'before'          => '',
				    'after'           => '',
				    'show_post_count' => false,
				    'echo'            => 1 );

                    wp_get_archives( $args );?>
												</ul>
                                  			</div>
                                  		</div>
                                  </div>
<?
}

function get_blog_blogrecent()
{

	$show_posts = 5;
	$count = 0;
?>
                                  <div id="sidebar-recent-top">
                                  		<div class="sidebar-block-bottom">
                                  			<div class="sidebar-block-cont">

                                                 <ul id="blog-recent">

                                                  <?
                                                    	$recent_posts = wp_get_recent_posts(20);
                                                    	foreach($recent_posts as $post){

                                                             if((check_blog_post($post["ID"])) and ($count < $show_posts))
                                                             {                                                             	echo "<li class=\"cat-item\"><a href=\"".get_permalink($post["ID"])."\">".$post["post_title"]."</a></li>";                                       $count++;

                                                             }

												 		}
												 ?>

												</ul>
                                  			</div>
                                  		</div>
                                  </div>
<?
}


function get_info_address()
{
	echo "<div id='info-address'>1750 El Camino Real,<br>Suite E<br>Grover Beach - CA.</div>";

}

function get_info_phone()
{

	echo "<div id='info-phone'>825-652-3645</div>";

}

function get_info_mail()
{

	echo "<div id='info-mail'>contact@vresponse.com</div>";

}

function subscribe($email)
{
		require_once('inc/MCAPI.class.php');
		$list_id = "327b836f92";
		$key ="02c68d101ce101fa3d86e781541ff0fd-us1";
		$api = new MCAPI($key);

		$merge_vars = array('FNAME'=>'', 'LNAME'=>'', 'INTERESTS'=>'');
		if($api->listSubscribe($list_id, $email, $merge_vars) == true)
		{
			// It worked!
			echo "<script type=\"text/javascript\">
			var cont = document.getElementById('content-text');
			cont.innerHTML = \"<br><br><br><center><font color='#004400'><b>Success! Check your email to confirm sign up.</b></font></center>\" ;
			</script>";

		}
		else
		{
			// An error ocurred, return error message
		    echo "<script type=\"text/javascript\">
			var cont = document.getElementById('content-text');
			cont.innerHTML = \"<br><br><br><center><font color='#ff0000'><b>Error: ".$api->errorMessage."</b></font></center>\" ;
		    </script>";
		}

}



function blog_get_content()
{

						while (have_posts()) : the_post();

                      ?>

                        <!-- POST BLOCK -->
                                        <div class="blog-post-top">
                                        <div class="blog-title-top">
                                                        <div class="blog-title-bot">
                                                                <div class="blog-title-cont">
                                                                    <div class="blog-title">
                                                                         <a href="<? the_permalink(); ?>">
                                                                                <? echo the_title(); ?>
                                                                                </a>
                                                                        </div>
                                                                    <div class="blog-title-posted">
                                                                                Posted by: <? the_author(); ?> - <? the_date(); ?>
                                                                        </div>
                                                                </div>
                                                        </div>
                                                </div>
                                    <div class="blog-post-bottom">
                                        <div class="blog-post-center">
                                            <div class="blog-post">
                                                <? the_content(); ?>

                                            </div>
                                        </div>
                                        <div class="blog-post-tags">
                                            <? the_tags('<b>Tags: </b>'); ?>
                                        </div>
                                        <div class="blog-post-comments">
                                                <span class="padd">Leave your comment</span>
                                                <a href="<? comments_link(); ?>"><? comments_number('0 comments','1 comment','% comments'); ?></a>
                                        </div>
                                    </div>
                                </div>
                        <!-- // -->

                       <? endwhile;
}

function get_breadcrumbs()
{
                echo "<span>You are here:</span>";
	            echo "<span id=\"text\">";
	                  	$mybreadcrumb = new breadcrumb_navigation_xt;
						// Display the breadcrumb
						$mybreadcrumb->display();
	            echo "</span>";

}

function get_bmarks()
{

				if( class_exists('Add_to_Any_Subscribe_Widget') ) {
						$A2opts = array(
				        "feedname" => "Vital responce",
				        "button_fname" => "fname",
				        "button_text" => "btext",
				        "before_widget" => "",
				        "after_widget" => "",
				        "feedurl" => get_bloginfo('rss2_url'));

						$a=new Add_to_Any_Subscribe_Widget;
						$a->display($A2opts);
				}
				echo "&nbsp;
				<script type='text/javascript' src='http://static.addtoany.com/menu/page.js'></script>
    			<a class='a2a_dd' href='http://www.addtoany.com/share_save'><img src='/wp-content/themes/VitalResponse/img/bookmarks.gif'></a>
                <script type='text/javascript'>
				a2a.init('page');
				</script>";

}

function get_home_top_text()
{      $id = TOP_TEXT_POST_ID;
      $text = get_post($id);
      echo $text->post_content;

}

function get_block_contactus()
{
?>

                                  <div id="sidebar-contactus">
                                  		<div class="sidebar-block-bottom">
                                  			<div class="sidebar-block-cont">
                                  				<div class="sidebar-block-text">
                                  				     <?
                                       					$id = CONTACTUS_TEXT_POST_ID;
														$text = get_post($id);
														echo $text->post_content;
			                                         ?>
                                  				</div>
                                  				<button id="sidebar-contactus-btn" onclick="window.location.href='/contact-us'"></button>
                                  			</div>
                                  		</div>
                                  </div>

<?
}

function get_welcome_post()
{
      $id = WELCOME_TEXT_POST_ID;
	  $text = get_post($id);
	  echo $text->post_content;

}

function get_neighborhood_post()
{

      $id = NEIGHBORHOOD_TEXT_POST_ID;
	  $text = get_post($id);
	  echo $text->post_content;

}

function get_blog_blogrecent_home()
{

	$show_posts = 2;
	$count = 0;

       $recent_posts = wp_get_recent_posts(20);
       foreach($recent_posts as $post){

	       if((check_blog_post($post["ID"])) and ($count < $show_posts))
	       {
	            if($count == 0)
	            {	                $divid = 'home_blog_post_left';
	            }
	            else
	            {	                $divid = 'home_blog_post_right';

	            }

	            echo "<div id='".$divid."'>
	                  <div class='post-img'>";

	          $str = "select * from `wp_posts` where `post_status` = \"inherit\" and `post_parent` = ".$post["ID"]." and `post_type` = \"attachment\"";

			  $qe = mysql_query($str);
			  if ((mysql_num_rows($qe) > 0) and ($row2 = mysql_fetch_object($qe)))
			  {

			      echo "<img src='".$row2->guid."' alt='".$post["post_title"]."' width='92' height='63'>";

			  }

	               echo "</div>
	               <div class='post-title'>
	               <a href='".get_permalink($post["ID"])."'>".$post["post_title"]."</a>
	               </div>
	               <div class='post-content'>";

	               echo prepare_q($post["post_content"], 90)."</div>
	               <div class='to_post'>
	               <a href='".get_permalink($post["ID"])."'>> Read Article</a>
	               </div>
	               </div>";
	               $count++;



		   }
       }

}

function check_redirect_url($url)
{

		if(substr_count($_SERVER['REDIRECT_URL'], $url) > 0) return true; else return false;


}

?>
